package com.jscloud.bigdata.zk.common;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;

/**
 * 创建临时节点
 */
public class ZooKeeperAPIForEPHEMERAL {
        public static void main(String[] args) throws Exception {
                //连接字符串
                String connectionstr = "192.168.1.110:2181, 192.168.1.111:2181, 192.168.1.112:2181";

                //指定重试策略
                ExponentialBackoffRetry retrypolicy = new ExponentialBackoffRetry(1000, 3, Integer.MAX_VALUE);

                //创建客户端对象
                CuratorFramework client = CuratorFrameworkFactory.newClient(connectionstr, retrypolicy);

                //启动客户端
                client.start();

                /**
                 *  创建一个允许所有访问的“临时节点”
                 */
                client.create()
                        .creatingParentContainersIfNeeded() //递归创建，如果没有父节点，自动创建父节点
                        .withMode(CreateMode.EPHEMERAL) //节点类型，临时节点
                        .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE) //设置ACL，和原生API相同
                        .forPath("/test02", "haha".getBytes());

                //client.close(); //如果创建临时节点时，不能关闭客户端，否则创建的临时节点就被删除了

        }
}
